//
//  SRMessageCenterViewController.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/3/28.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRMessageCenterViewController.h"
#import "SRMessageListTableViewCell.h"
#import "SRMessageListModel.h"
#import "SRPersonalServices.h"

#import <MJRefresh/MJRefresh.h>

@interface SRMessageCenterViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *items;
@property (assign, nonatomic) NSInteger page;

@end

@implementation SRMessageCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        // Do any additional setup after loading the view.
    
    self.title = @"消息中心";
    [self setupData];
    [self setupView];
    
    [self showLoadingViewWithCompletedBlock:nil];
    [self queryData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
}

#pragma mark - setup

- (void)setupData {
    self.page = 1;
    self.items = [@[] mutableCopy];
}

- (void)setupView {
    
    WEAKSELF
    
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 150;
    
    [self.tableView registerNib:[UINib nibWithNibName:kSRMessageListTableViewCell bundle:nil] forCellReuseIdentifier:kSRMessageListTableViewCell];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.top.equalTo(self.view);
    }];
    self.view.backgroundColor = self.tableView.backgroundColor = css.color_2;
    
    
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf queryData];
    }];
    
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf queryData];
    }];
}

#pragma mark - Query Data 


- (void)queryData{
    WEAKSELF
    [SRPersonalServices querySystemMessageListPage:self.page successBlock:^(NSInteger statusCode, NSString *message, id responseDataObject) {
        [weakSelf hideLoadingView];
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        
        if (statusCode == kFZRequestStatusCodeSuccess && responseDataObject) {
            
            NSArray *addItems = @[];
            if ([responseDataObject isKindOfClass:[NSArray class]]) {
                addItems = responseDataObject;
            }
            
            if (weakSelf.page == 1) {
                [weakSelf.items removeAllObjects];
            }
            
            if ([addItems count] <= 0) {
                    //没有更多
                [weakSelf.tableView.footer noticeNoMoreData];
                if (weakSelf.page == 1) {
                    [weakSelf emptyLoadingViewWithTitle:nil];
                }
                
            } else {
                weakSelf.page += 1;
                [weakSelf.items addObjectsFromArray:addItems];
            }
            
            [weakSelf.tableView reloadData];
            
        } else {
            if (weakSelf.page == 1) {
                [weakSelf failedLoadingViewWithTitle:nil];
            }
        }
    
    } failureBlock:^(id responseObject, NSError *error) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];

        if ([weakSelf.items count] <= 0) {
            [weakSelf failedLoadingViewWithTitle:nil];
        }

    }];
    
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SRMessageListTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kSRMessageListTableViewCell forIndexPath:indexPath];
    
    SRMessageListModel *item = self.items[indexPath.row];
    [cell updateCellInfo:item];
    
    return cell;
}


#pragma mark - UITableViewDelegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


@end
